const {ccclass, property} = cc._decorator;

enum NodeStatus {
    Show,
    Hide,
    Toggle,
    Destroy
}

@ccclass
export default class NewClass extends cc.Component {

    @property({type:cc.AudioClip})
    seClick = null;

    @property(cc.Node)
    controlNode = null;

    @property({type: cc.Enum(NodeStatus)})
    controlNodeStatus : NodeStatus = NodeStatus.Show;

    @property(cc.Prefab)
    newPrefab : cc.Prefab = null;

    @property(cc.Node)
    newPrefabParent : cc.Node = null;

    @property([cc.Component.EventHandler])
    clickEvents : cc.Component.EventHandler[] = [];
    
    onLoad() {
        this.node.on('touchend', (event) => {
            this.onClick(event);
        });
    }

    onClick(event){
        let _status = this.controlNodeStatus;
        if(this.seClick)
           if(isSound){
            cc.audioEngine.playEffect(this.seClick, false);
           }
        if(this.controlNode){
            if(_status == NodeStatus.Show)
                this.controlNode.active = true;
            
            if(_status == NodeStatus.Hide)
                this.controlNode.active = false;

            if(_status == NodeStatus.Toggle)
                this.controlNode.active = !this.controlNode.active;

            if(_status == NodeStatus.Destroy)
                this.controlNode.destroy();
        }

        if(this.newPrefab){
            if(this.newPrefabParent) {
               let prefab = cc.instantiate(this.newPrefab);
              prefab.parent = this.newPrefabParent;
            } else {
                cc.log("[ExButton] newPrefabParent error");
            }
        }
        if(this.clickEvents !== null)
            cc.Component.EventHandler.emitEvents(this.clickEvents, event);
    }

}
